/**
 * 以后这些函数库全部都需要载入
 * 加油吧小伙伴们 by Mike/大当家
 */

let far         = com.far.x;
let os          = far.utils.script.OsKit;
let OProtocol   = far.game.define.OProtocol;


let __dir__;


/**
 * 默认存redis
 * @param ob
 */
function saveObject(ob) {
    let keys = Object.keys(ob);
    //声明父类继承下来的属性需要存储到的地方
    ob.data["__attr__"] = {};
    for( let k in keys ){
        if( keys[k].startsWith("_") ){
            //凡是_开头的都是父类属性，子类都要存储
            ob.data["__attr__"][keys[k]] = ob[keys[k]];
        }
    }
    return os.cache.save(ob.get("dtable"), ob.get("id"), JSON.stringify(ob.data));
}

function restoreObject(ob) {
    let data = os.os.cache.restore(ob.get("dtable"), ob.get("id"));
    data = JSON.parse(data);
    let keys = Object.keys(data.__attr__);
    //装载属性
    for(let e in keys){
        let key =  keys[e];
        ob[key] = data.__attr__[key];
    }
    //删除读取的属性
    delete data["__attr__"];
    ob.data = data;
   return ob;
}


function newObj() {
    let arg = arguments;
    if( arg.length==2 ){
        //arg 1是配置文件
        let obj = os.create(arg[0]),
            {datas} = os.importx(arg[1]);
        let dueKey = Object.keys(datas.due),
            tempKey = Object.keys(datas.temp);

        for( let dk in dueKey ){
            obj.set( dueKey[dk], datas.due[dueKey[dk]])
        }

        for( let tk in tempKey ){
            obj.set( tempKey[tk], datas.temp[dueKey[dk]])
        }
        return obj;
    }else if(arg.length==1){
        return os.create(arg[0]);
    }
}



function getObjects() {
    return os.getObjects();
}


function objectp(ob) {
    let type =  Object.prototype.toString.call(ob);
    if( type=="[object object]" ) return true;
    return false;
}
//判断方式 https://www.cnblogs.com/yadiblogs/p/10750775.html
function arrayp(ob) {
    let type =  Object.prototype.toString.call(ob);
    if( type=="[object Array]" ) return true;
    return false;
}

function stringp(ob) {
    let type =  Object.prototype.toString.call(ob);
    if( type=="[object String]" ) return true;
    return false;
}


function getUser(account) {
    let obs = os.getUsers();
    for (let e in obs) {
        if (obs[e].get("account") == account) {
            return obs[e];
        }
    }
    return 0;
}

function thisObject() {
    return os.thisObject();
}

function D(file) {
    return os.D(file);
}

//占时不处理
function notifyFail(str){

}


function undefinedp(val) {
   return  typeof(val) == "undefined";
}

function baseName(ob) {
    // let fileName = ob.get()
    // "meScript":"main/main.js"
}


//-----------------------------------------------------------------------------
/**
 * 在一个区间生成随机数
 * @param min
 * @param max
 * @returns {number}
 */
function rangeRondom(min, max) {
    return Math.floor(Math.random() * (max - min + 1) + min);
}

/**
 * 深拷贝
 * @param obj
 * @returns {*[]}
 */
function deepClone(obj) {
    let objClone = Array.isArray(obj) ? [] : {};
    if (obj && typeof obj === "object" && obj.hasOwnProperty) {
        for (key in obj) {
            if (obj.hasOwnProperty(key)) {
                //判断ojb子元素是否为对象，如果是，递归复制
                if (obj[key] && typeof obj[key] === "object") {
                    objClone[key] = deepClone(obj[key]);
                } else {
                    //如果不是，简单复制
                    objClone[key] = obj[key];
                }
            }
        }
    }
    return objClone;
}


/**
 * 随机从一个数组里面抽取一个元素
 * @param arr
 * @returns {*}
 */
function getRandomOneOfArr(arr) {
    return arr[Math.floor(Math.random() * arr.length)];
}





/**
 * 数字货币转换
 * @param value
 * @returns {string}
 */
function transformNumToCh(value) {
    let newValue = ['', '', ''];
    let fr = 1000;
    const ad = 1;
    let num = 3;
    let fm = 1;
    while (value / fr >= 1) {
        fr *= 10;
        num += 1;
        console.log('数字', value / fr, 'num:', num);
    }
    if (num <= 4) { // 千
        newValue[1] = '千';
        newValue[0] = parseInt(value / 1000) + '';
    } else if (num <= 8) { // 万
        let text1 = parseInt(num - 4) / 3 > 1 ? '千万' : '万';
        // tslint:disable-next-line:no-shadowed-variable
        fm = '万' === text1 ? 10000 : 10000000;
        newValue[1] = text1;
        newValue[0] = (value / fm) + '';
    } else if (num <= 16) {// 亿
        let text1 = (num - 8) / 3 > 1 ? '千亿' : '亿';
        text1 = (num - 8) / 4 > 1 ? '万亿' : text1;
        text1 = (num - 8) / 7 > 1 ? '千万亿' : text1;
        // tslint:disable-next-line:no-shadowed-variable
        let fm = 1;
        if ('亿' === text1) {
            fm = 100000000;
        } else if ('千亿' === text1) {
            fm = 100000000000;
        } else if ('万亿' === text1) {
            fm = 1000000000000;
        } else if ('千万亿' === text1) {
            fm = 1000000000000000;
        }
        newValue[1] = text1;
        newValue[0] = parseInt(value / fm) + '';
    }
    if (value < 1000) {
        newValue[1] = '';
        newValue[0] = value + '';
    }
    return newValue.join('');
}
